package demo3;

import java.util.Arrays;
import java.util.Comparator;

class Student implements Comparable<Student>{
    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        return this.age - o.age;
    }
}

class Agecomparator implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        return o1.age - o2.age;
    }
}

class Namecomparator implements Comparator<Student>{

    @Override
    public int compare(Student o1, Student o2) {
        return o1.name.compareTo(o2.name);
    }
}
public class Test {


    public static void bubblesort(Comparable[] comparables){
        for (int i = 0; i < comparables.length - 1; i++) {
            for (int j = 0; j < comparables.length-1-i; j++) {
                if(comparables[j].compareTo(comparables[j+1]) < 0){
                    Comparable tmp = comparables[j];
                    comparables[j] = comparables[j+1];
                    comparables[j+1] = tmp;
                }
            }
        }
    }
    public static void main(String[] args) {
//        Student students = new Student();
        Student students1 = new Student("zhangsan", 10);
        Student students2 = new Student("lisi", 2);
//        students[2] = new Student("王五", 18);


        //Arrays.sort(students);
        //bubblesort(students);

        //System.out.println(Arrays.toString(students));
        Agecomparator agecomparator = new Agecomparator();
        System.out.println(agecomparator.compare(students1,students2));

        Namecomparator namecomparator = new Namecomparator();
        System.out.println(namecomparator.compare(students1,students2));

    }

}
